package com.cazor.dg.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cazor.common.utils.DateUtils;
import javax.annotation.Resource;

import com.cazor.dg.domain.DgHisDataReqList;
import com.cazor.dg.domain.DgParamDataSensor;
import com.cazor.dg.service.IDgHisDataReqListService;
import org.springframework.stereotype.Service;
import com.cazor.dg.mapper.DgHisDataSensorMapper;
import com.cazor.dg.domain.DgHisDataSensor;
import com.cazor.dg.service.IDgHisDataSensorService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 历史数据上传传感器完成列Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-16
 */
@Service
public class DgHisDataSensorServiceImpl extends ServiceImpl<DgHisDataSensorMapper, DgHisDataSensor> implements IDgHisDataSensorService
{
    @Resource
    private DgHisDataSensorMapper dgHisDataSensorMapper;
    @Resource
    private IDgHisDataReqListService dgHisDataReqListService;

    /**
     * 查询历史数据上传传感器完成列
     * 
     * @param id 历史数据上传传感器完成列主键
     * @return 历史数据上传传感器完成列
     */
    @Override
    public DgHisDataSensor selectDgHisDataSensorById(Long id)
    {
        return dgHisDataSensorMapper.selectDgHisDataSensorById(id);
    }

    /**
     * 查询历史数据上传传感器完成列列表
     * 
     * @param dgHisDataSensor 历史数据上传传感器完成列
     * @return 历史数据上传传感器完成列
     */
    @Override
    public List<DgHisDataSensor> selectDgHisDataSensorList(DgHisDataSensor dgHisDataSensor)
    {
        return dgHisDataSensorMapper.selectDgHisDataSensorList(dgHisDataSensor);
    }

    /**
     * 新增历史数据上传传感器完成列
     * 
     * @param dgHisDataSensor 历史数据上传传感器完成列
     * @return 结果
     */
    @Override
    public int insertDgHisDataSensor(DgHisDataSensor dgHisDataSensor)
    {
        dgHisDataSensor.setCreateTime(DateUtils.getNowDate());
        return dgHisDataSensorMapper.insertDgHisDataSensor(dgHisDataSensor);
    }

    /**
     * 修改历史数据上传传感器完成列
     * 
     * @param dgHisDataSensor 历史数据上传传感器完成列
     * @return 结果
     */
    @Override
    public int updateDgHisDataSensor(DgHisDataSensor dgHisDataSensor)
    {
        dgHisDataSensor.setUpdateTime(DateUtils.getNowDate());
        return dgHisDataSensorMapper.updateDgHisDataSensor(dgHisDataSensor);
    }

    /**
     * 批量删除历史数据上传传感器完成列
     * 
     * @param ids 需要删除的历史数据上传传感器完成列主键
     * @return 结果
     */
    @Override
    public int deleteDgHisDataSensorByIds(Long[] ids)
    {
        return dgHisDataSensorMapper.deleteDgHisDataSensorByIds(ids);
    }

    /**
     * 删除历史数据上传传感器完成列信息
     * 
     * @param id 历史数据上传传感器完成列主键
     * @return 结果
     */
    @Override
    public int deleteDgHisDataSensorById(Long id)
    {
        return dgHisDataSensorMapper.deleteDgHisDataSensorById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    public void saveRecords4Failed(DgParamDataSensor paramDataSensor, int dataCount, int successCount
            , List<DgHisDataReqList> dgHisDataReqList) {
        DgHisDataSensor sensor = new DgHisDataSensor();
        sensor.setDeviceId(paramDataSensor.getDeviceId());
        sensor.setSensorId(paramDataSensor.getSensorId());
//        sensor.setName(paramDataSensor.get);
        sensor.setDataCount(dataCount);
        sensor.setSuccessCount(successCount);
        sensor.setCreateBy("sys");
        sensor.setCreateTime(DateTime.now());
//        sensor.setUpdateBy();
//        sensor.setUpdateTime();

        if (CollectionUtil.isNotEmpty(dgHisDataReqList)) {
            dgHisDataReqListService.saveBatch(dgHisDataReqList);
        }
        this.save(sensor);
    }
}
